package com.ltmall.api.dao.impl;

import com.ltmall.api.bean.News;
import com.ltmall.api.dao.INewsDAO;
import com.ltmall.api.util.PageUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.sql.SQLException;
import java.util.List;

public class NewsDAO extends BaseEntityDAO<News> implements INewsDAO {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<News> newsList(final int userID,final PageUtil page,final int typeA) {
		// TODO Auto-generated method stub
		final String HQL = "from News news where news.userID = ? and news.typeA = ? order by news.opTime desc  ";
		List<News> list = this.getHibernateTemplate().execute(
				new HibernateCallback<List>() {
					public List doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(HQL);
						query.setParameter(0, userID);
						query.setParameter(1, typeA);
						// 分页
						query.setFirstResult(
								(page.getCur_page() - 1) * page.getNumPerPage())
								.setMaxResults(page.getNumPerPage());
						return query.list();
					}
				});
		return list;
	}
	
}
